<template>
  <view>
   <view class="top_search">
     <uni-search-bar placeholder="请输入搜索内容" v-model="searchKey" focus=true bgColor="#fff"  radius="30" cancelButton="none"/>
   </view>
   <view class="suggest_list" v-if="searchKey!==''">
     <view class="suggest_item" v-for="(item,index) in searchSuggest" :key="index" @click="goGoodsDetail(item)">
       <view class="suggest_text">
         {{item.goods_name}}
       </view>
       <uni-icons type="right" size="16"></uni-icons>
     </view>
   </view>
    <view class="search_history" v-else>
      <view class="search_history_top">
        <text>搜索历史</text>
        <uni-icons type="trash-filled" size="22" @click="clearSearchHistory"></uni-icons>
      </view>
      <view class="search_history_bottom">
        <view class="search_history_bottom_item" v-for="(item,index) in searchHistory" :key="index" @click="clickTag(item)">
          <uni-tag :text="item" ></uni-tag>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        searchKey:"",
        timer:'',
        searchSuggest:[],
        searchHistory:[]
      };
    },
    onLoad() {
     this.searchHistory= uni.getStorageSync("searchSuggest")||[]
    },
    watch:{
       searchKey:{
         handler(newval,oldval){
           clearTimeout(this.timer)
           this.timer=setTimeout(()=>{
             if(newval.trim()===''){
              return false
             }
             uni.$http.get('/api/public/v1/goods/qsearch',{query:newval.trim()}).then((res)=>{
               if(res.data.meta.status!==200) uni.$showMsg("获取搜索建议失败！")
               this.searchSuggest=res.data.message
               this.searchHistory.unshift(newval.trim())
               let s=new Set(this.searchHistory)
               this.searchHistory=[...s]
               uni.setStorageSync("searchSuggest",this.searchHistory)
             })
           },500)
         }
       }
    },
    methods:{
      clickTag(item){
        uni.navigateTo({
          url:"/subpkg/goods_list/goods_list?query="+item
        })
      },
      clearSearchHistory(){
        this.searchHistory=[]
        uni.clearStorageSync("searchSuggest")
      },
      goGoodsDetail(item){
        uni.navigateTo({
          url:"/subpkg/goods_detail/goods_detail?goods_id="+item.goods_id
        })
      }
    }
  }
</script>

<style lang="scss">
.top_search{
  height:150rpx;
  background-color: #c00000;
  position: sticky;
  z-index: 999;
  top: 0;
}
.suggest_item{
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px dashed #ccc;
  padding: 20rpx 10rpx ;

  .suggest_text{
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    font-size: 12px;
    color: #333;
  }
}
  .search_history{
    padding: 10rpx;
    .search_history_top{
      padding: 20rpx 0;
      display: flex;
      font-size: 14px;
      justify-content: space-between;
      border-bottom: 1px solid #ccc;
    }
    .search_history_bottom{
      display: flex;
      flex-wrap: wrap;
      .search_history_bottom_item{
        margin-top: 5px;
        margin-right: 5px;
        .uni-tag {
          background-color: #ccc!important;
          border: 1px solid #ccc;
        }
      }
     
    }
  }
</style>
